package com.xh.audit.controller.project;

import com.mybatisflex.core.paginate.Page;
import com.xh.audit.entity.project.AuditIssues;
import com.xh.audit.service.AuditIssuesService;
import com.xh.audit.vo.webform.AuditIssuesQuery;
import com.xh.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 审计问题 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/audit-issues")
@RequiredArgsConstructor
@Validated
public class AuditIssuesController {

    private final AuditIssuesService auditIssuesService;

    @PostMapping("/save")
    public R<AuditIssues> save(@RequestBody
                               @Validated
                               AuditIssues tosave) {
        boolean saveFlag = auditIssuesService.saveOrUpdate(tosave);

        if (saveFlag) {
            return R.success(auditIssuesService.getById(tosave.getId()));
        }
        return R.failed("保存失败");
    }

    @PostMapping("/queryByPage")
    public R<Page<AuditIssues>> query(@RequestBody
                                      @Validated
                                      AuditIssuesQuery queryPage) {

        Page<AuditIssues> page1 = auditIssuesService.page(queryPage.getPageInfo(), queryPage.queryWrapperBuild());
        return R.success(page1);
    }

    /*@PostMapping("/info/{id}")
    public R<AuditIssues> info(@PathVariable
                               @Validated
                               @NotNull(message = "id不能空")
                                       Integer id) {

        AuditIssues byId = auditIssuesService.getById(id);
        return R.success(byId);
    }*/

    @PostMapping("/del")
    public R<AuditIssues> del(@RequestBody
                              @Validated
                              @NotEmpty(message = "ids 集合不能空")
                              List<Integer> ids) {
        auditIssuesService.removeByIds(ids);
        return R.success("删除成功");
    }

    @PostMapping("/querybyIds")
    public R<List<AuditIssues>> querybyIds(@RequestBody
                                           @Validated
                                           @NotEmpty(message = "ids 集合不能空")
                                           List<Integer> ids) {

        return R.success(auditIssuesService.listByIds(ids));
    }


}
